from rest_framework import serializers
from ninja import Schema, Field

from models.models import Advertisement
from water_applets import settings


class AdvertisementSerializer(serializers.ModelSerializer):
    picture = serializers.SerializerMethodField()

    def get_picture(self, obj):
        header = "http://"
        if settings.WEB_SERVER_IS_HTTPS:
            header = "https://"
        return f'{header}{settings.WEB_SERVER_IPV4}:{settings.WEB_SERVER_PORT}/{obj.picture}'

    class Meta:
        model = Advertisement
        fields = "__all__"


class IndexAdAndAdvertisementSchema(Schema):
    type: str = Field(description="类型[user_index|user_equity|agent_index|agent_package]", pattern=r"[user_index|user_equity|agent_index|agent_package]")
